export default class Marker {
	constructor(viewer) {
		this.viewer = viewer;
		// 是否允许添加
		// this.isAddCustomItemActtivated = true;
		this._init();
	}
	_init() {
		this.userDataMap = new Map();
		let drawableConfig = new Glodon.Bimface.Plugins.Drawable.DrawableContainerConfig();
		drawableConfig.viewer = this.viewer;
		//容器
		this.drawableContainer = new Glodon.Bimface.Plugins.Drawable.DrawableContainer(drawableConfig);
	}
	add(options = {}) {
		try {
			let {
				position,
				text,
				textSize,
				textColor,
				id,
				showBackground,
				backgroundColor,
				imagePath,
				onlyText,
				textType,
				userData,
				textShow,
				coordinateType
			} = options;

			let config = new Glodon.Bimface.Plugins.Drawable.CustomItemConfig();
			let rectangle = document.createElement('div');
			//自定义样式，支持Html的任意dom元素
			if (userData) {
				this.userDataMap.set(id, userData);
			}

			if (!onlyText && !imagePath) onlyText = true;

			if (!textShow) {
				text = '';
			}

			// innerhtml
			if (onlyText) {
				rectangle.innerText = text;
				rectangle.style.width = '100px';
				rectangle.style.height = '32px';
				rectangle.style.lineHeight = '32px';
				rectangle.style.border = 'solid';
				rectangle.style.borderWidth = '1px';
				rectangle.style.borderColor = '#32D3A6';
			} else {
				if (textType === 'Top') {
					rectangle.innerHTML = `<div>${text}</div><img src=${imagePath}  width="49"/>`;
				} else {
					rectangle.innerHTML = `<img src=${imagePath}  width="49"/><div>${text}</div>`;
				}
			}
			rectangle.style.fontSize = textSize ? textSize + 'px' : '16px';

			rectangle.style.color = textColor || '#fff';
			rectangle.style.borderRadius = '3px';
			showBackground
				? (rectangle.style.background = backgroundColor || '#F2F2F2')
				: (rectangle.style.background = '#F2F2F2');
			rectangle.style.textAlign = 'center';
			config.content = rectangle;
			//设置可以拖拽
			config.draggable = false;
			config.offsetY = -32;
			config.viewer = this.viewer;
			//增加一个Tip提示
			config.tooltip = '点击左键删除';
			// console.log(this.viewer._getPositionByLatLon);
			console.log(coordinateType);
			if (this.viewer._getPositionByLatLon && coordinateType == 1) {
				let transferPosition = this.viewer._getPositionByLatLon({
					lat: position[0],
					lon: position[1],
					height: position[2]
				});
				config.worldPosition = transferPosition;
				console.log(transferPosition);
			} else {
				config.worldPosition = {
					x: position[0],
					y: position[1],
					z: position[2]
				};
			}

			config.id = id;

			//生成customItem实例
			let customItem = new Glodon.Bimface.Plugins.Drawable.CustomItem(config);
			//自定义标签的鼠标左键事件
			customItem.onClick((item) => {
				this.delete(id);
			});
			//自定义标签的鼠标右键事件
			customItem.onRightClick(function (item) {
				// alert('right click');
			});
			customItem.onEndDrag(function (item) {
				console.log(JSON.stringify(item.worldPosition));
			});
			// 添加自定义标签
			this.drawableContainer.addItem(customItem);
			//设置Tip的样式
			customItem.setTooltipStyle({
				border: '1px',
				top: '-33px'
			});
		} catch (error) {
			console.log(error);
		}
	}
	delete(id) {
		this.drawableContainer.removeItemById(id);
		this.userDataMap.delete(id);
	}
	clear() {
		this.drawableContainer.clear();
		this.userDataMap.clear();
	}
}
